package org.jiangnantaste.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jiangnantaste.bo.DeliveryOrderStatusBo;
import org.jiangnantaste.domain.Image;
import org.jiangnantaste.domain.MenuItem;
import org.jiangnantaste.util.HttpUtil;
import org.jiangnantaste.util.ImageDispose;
import org.jiangnantaste.util.ImageHelper;
import org.jiangnantaste.vo.MenuItemVo;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListAllMenuItemsActivity extends ListActivity{
	private ListView myView;
	List<MenuItem> imlist;
	List<MenuItemVo> vimlist;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try {
			setTitle("江南味道");
			
			String url = HttpUtil.BASE_URL+"facade/listAllMenuItemsFacade";
			String result = HttpUtil.queryStringForPost(url);
	
			imlist = parseMenuItemList(result);
			MyAdapter adapter = new MyAdapter(this);
			myView = new ListView(this);
			myView.setAdapter(adapter);
			setContentView(myView);
		} catch(Exception e){			
		}		
	}
	
	 private List<Map<String, Object>> getData(List<MenuItem> originalList) {
	        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	        
	        for (int i=0; i < originalList.size(); i++){
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("菜品序号", originalList.get(i).getMenuItemId());
					map.put("菜品名", originalList.get(i).getMenuName());
					map.put("状态", originalList.get(i).isActive());
					list.add(map);
	           }
	        return list;
	    }
	
	public List<MenuItem> parseMenuItemList(String json) throws Exception{
		ArrayList<MenuItem> list = new ArrayList<MenuItem>();
		JSONTokener jsonParser = new JSONTokener(json);
		JSONArray arr = (JSONArray) jsonParser.nextValue();
		for (int i = 0; i< arr.length(); i++){
			MenuItem od = new MenuItem();
			JSONObject jo = arr.getJSONObject(i);
			od.setMenuItemId(jo.getLong("menuItemId"));
			od.setMenuName(jo.getString("menuName"));
			od.setActive(jo.getBoolean("active"));
			JSONObject jsimg = jo.getJSONObject("image");
			Image img = new Image();
			img.setImageId(jsimg.getLong("imageId"));
			img.setImageName(jsimg.getString("imageName"));
			img.setPathName(jsimg.getString("pathName"));
			img.setImageData(ImageHelper.decode(jsimg.getString("imageData")));
			img.setActive(jsimg.getBoolean("active"));
			od.setImage(img);
			od.setImageId(img.getImageId());
			list.add(od);
		}
		return list;
	}

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
         
        Log.v("JerryDebug::ListViewClick::", (String)imlist.get(position).getMenuName());
    }
    public void showInfo(int position){
        new AlertDialog.Builder(this)
        .setTitle("江南味道")
        .setMessage(imlist.get(position).toString())
        .setPositiveButton("", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        })
        .show();
         
    }
    
    public void refleshMenuItem(int position){
	    String url = HttpUtil.BASE_URL+"facade/updateMenuItemFacade";
		SharedPreferences pres = getSharedPreferences("user_msg", MODE_WORLD_READABLE);
		String userid = pres.getString("userid","");
		long deliveryorderid = imlist.get(position).getImageId();
		url = url + "?employeeid=" + userid + "&deliveryorderid=" + deliveryorderid;
		String result = HttpUtil.queryStringForGet(url);
		try {
			imlist = parseMenuItemList(result);
			myView.refreshDrawableState();
		} catch (Exception e){
			
		}
    }
     
     
     
    public final class ViewHolder{
        public TextView menuItemId;
        public TextView menuItemName;
        public TextView active;
        public ImageView imageData;
        public Button viewBtn;
    }
     
    public class MyAdapter extends BaseAdapter{
 
        private LayoutInflater mInflater;
         
         
        public MyAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return imlist.size();
        }
 
        @Override
        public Object getItem(int position) {
            return imlist.get(position);
        }
 
        @Override
        public long getItemId(int position) {
        	return imlist.get(position).getImageId();
        }
 
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
             
            ViewHolder holder = null;
            if (convertView == null) {
                 
                holder=new ViewHolder();  
                 
                convertView = mInflater.inflate(R.layout.menuitem_list, null);
                holder.menuItemId = (TextView)convertView.findViewById(R.id.menuitemid);
                holder.menuItemName = (TextView)convertView.findViewById(R.id.menuname);
                holder.active = (TextView)convertView.findViewById(R.id.menuitem_active);
                holder.imageData = (ImageView)convertView.findViewById(R.id.menuitem_imageData);
                holder.viewBtn = (Button)convertView.findViewById(R.id.view_menuitem_btn);
                convertView.setTag(holder);
            }else {
                 
                holder = (ViewHolder)convertView.getTag();
            	}            
             
            holder.menuItemId.setText(imlist.get(position).getImageId()+"");
            holder.menuItemName.setText((String)imlist.get(position).getMenuName());
            holder.active.setText(imlist.get(position).isActive()+"");
            holder.imageData.setImageBitmap(ImageDispose.getPicFromBytes(imlist.get(position).getImage().getImageData(), new BitmapFactory.Options()));
            holder.viewBtn.setOnClickListener(new View.OnClickListener() {
                 
                @Override
                public void onClick(View v) {
                    showInfo(position);                 
                }
            });
            
          
             
            return convertView;
        }
         
    }
}
